//
//  GHEditingViewC.m
//  Models
//
//  Created by lanou3g on 15/10/30.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "GHEditingViewC.h"
#import "WYTTimeTool.h"

#define kSelfViewW self.frame.size.width
#define kSelfViewH self.frame.size.height

@interface GHEditingViewC ()

@property (nonatomic, assign) NSInteger authorLabelFontSize;

@property (nonatomic, assign) CGFloat showLabelWidth;

@end

@implementation GHEditingViewC

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addViews];
    }
    return self;
}
- (void)addViews
{
    self.authorLabelFontSize = 11;
    if ([WYTDevicesTool iPhone6_iPhone6s]) {
        self.authorLabelFontSize = 10;
    } else if ([WYTDevicesTool iPhone320_DEVICES]) {
        self.authorLabelFontSize = 9;
    }
#pragma mark - 顶部label的控制
    // 图片
    self.pic_img = [[UIImageView alloc] initWithFrame:CGRectMake(kSelfViewW * 0.05, kSelfViewH * 0.09, kSelfViewW *0.9, kSelfViewW *0.9 / 4 * 3)];
    //self.pic_img.backgroundColor = [UIColor grayColor];
    UIImage *img = [UIImage imageNamed:@"imgPlaceHolderEVC"];
    self.pic_img.layer.contents = (id)img.CGImage;
    self.pic_img.contentMode = UIViewContentModeScaleAspectFill;
    self.pic_img.layer.masksToBounds = YES;
    [self addSubview:_pic_img];
    
    // touchImageButton
    self.touchImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.touchImageButton.frame = CGRectMake(kSelfViewW * 0.05, kSelfViewH * 0.09, kSelfViewW *0.9, kSelfViewW *0.9 / 4 * 3);
    [self addSubview:self.touchImageButton];
    
    // title
    self.titleTextV = [[LYLPlaceHoderTextView alloc] initWithFrame:CGRectMake(kSelfViewW * 0.05, CGRectGetMaxY(self.pic_img.frame) * 1.05, kSelfViewW * 0.32, kSelfViewH * 0.36)];
    self.titleTextV.placeholder = @"添加标题";
    NSInteger titleTextVFontSize = 28;
    if ([WYTDevicesTool iPhone6_iPhone6s]) {
        titleTextVFontSize = 26;
    }else{
        titleTextVFontSize = 24;
    }
    self.titleTextV.font = [UIFont boldSystemFontOfSize:titleTextVFontSize];
    self.titleTextV.textColor = [UIColor colorWithRed:0.25 green:0.25 blue:0.25 alpha:1.0f];
    [self addSubview:_titleTextV];
    
    // 横线
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.titleTextV.frame) * 1.1, CGRectGetMaxY(self.pic_img.frame) * 1.05, 1, kSelfViewH * 0.36)];
    self.lineView.backgroundColor = [UIColor grayColor];
    [self addSubview:_lineView];
    
    // 内容
    self.summaryTextV = [[LYLPlaceHoderTextView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.lineView.frame) * 1.1, CGRectGetMaxY(self.pic_img.frame) * 1.05, kSelfViewW* 0.5, kSelfViewH * 0.36)];
    self.summaryTextV.placeholder = @"写点什么...";
    NSInteger summaryTextVFontSize = 20;
    if ([WYTDevicesTool iPhone6_iPhone6s]) {
        summaryTextVFontSize = 18;
    }else{
        summaryTextVFontSize = 16;
    }
    self.summaryTextV.font = [UIFont boldSystemFontOfSize:summaryTextVFontSize];
    self.summaryTextV.textColor = [UIColor colorWithRed:0.25 green:0.25 blue:0.25 alpha:1.0f];
    [self addSubview:_summaryTextV];
    
#pragma mark - 底部label的控制
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, kSelfViewH * 0.926, kSelfViewW, kSelfViewH * 0.056)];
    [self addSubview:bottomView];
    
    //作者
    self.authorLabel = [[UILabel alloc] initWithFrame:CGRectMake((kSelfViewW - 140 - kSelfViewH * 0.028)/2 + kSelfViewH * 0.028,0, 140, kSelfViewH*0.028)];
//    self.authorLabel.text = [WYTTimeTool currentDate_yearMonthDateHourMinutesSeparatByDiagonals];
    self.authorLabel.text = @"2015/10/31 12:09pm ";
    self.authorLabel.textColor = [UIColor colorWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0f];
    self.authorLabel.font = [UIFont systemFontOfSize:self.authorLabelFontSize];
    self.showLabelWidth = [self getWidthString:self.authorLabel.text Font:self.authorLabelFontSize];
    self.authorLabel.frame = CGRectMake((kSelfViewW - self.showLabelWidth - kSelfViewH * 0.028)/2 + kSelfViewH * 0.028, 0, self.showLabelWidth, kSelfViewH*0.028);
    self.authorLabel.textAlignment = NSTextAlignmentLeft;
    //    self.authorLabel.backgroundColor = [UIColor cyanColor];
    [bottomView addSubview:self.authorLabel];
    
    // 天气图标
    self.weatherPicImg = [[UIImageView alloc] initWithFrame:CGRectMake((kSelfViewW - self.showLabelWidth - kSelfViewH*0.028)/2, 0, kSelfViewH*0.028, kSelfViewH*0.028)];
    self.weatherPicImg.image = [UIImage imageNamed:@"heavyRain_gray"];
    [bottomView addSubview:self.weatherPicImg];

    UIButton *weatherButton = [UIButton buttonWithType:UIButtonTypeCustom];
    weatherButton.frame = CGRectMake(0, kSelfViewH * 0.926, kSelfViewW, kSelfViewH * 0.056);
    weatherButton.backgroundColor = [UIColor clearColor];
    [weatherButton addTarget:self action:@selector(weatherButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:weatherButton];
    
    // 地址
    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.authorLabel.frame), CGRectGetWidth(bottomView.frame), CGRectGetHeight(bottomView.frame) * 0.5)];
    self.addressLabel.textAlignment = NSTextAlignmentCenter;
    self.addressLabel.text = @"北京市地铁5号线，天通苑南";
    self.addressLabel.font = [UIFont systemFontOfSize:self.authorLabelFontSize];
    self.addressLabel.textColor = [UIColor colorWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0f];

    [bottomView addSubview:self.addressLabel];
    
    UISwipeGestureRecognizer *swipToHideKeyboard = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipToHideKeyboardChick:)];
    swipToHideKeyboard.direction = UISwipeGestureRecognizerDirectionDown;
    [self addGestureRecognizer:swipToHideKeyboard];
    
    
    UISwipeGestureRecognizer *textViewSwipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(textViewHasBeenSwipeUp:)];
    textViewSwipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    
    [self addGestureRecognizer:textViewSwipeUp];
}

- (void)swipToHideKeyboardChick:(UISwipeGestureRecognizer *)sender
{
    [self endEditing:YES];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"SWIPETOHIDEKEYBOARD" object:nil userInfo:nil];
}
- (void)textViewHasBeenSwipeUp:(UISwipeGestureRecognizer *)sender{
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"SWIPEUPTODESK" object:nil userInfo:nil];
}

- (void)weatherButtonDidClicked:(UIButton *)sender{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"TAPTOJUMPINTODIARYINFOVIEW" object:nil userInfo:nil];
}

- (CGFloat)getWidthString:(NSString *)string Font:(NSInteger)font
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName];
    CGRect labelFrame = [string boundingRectWithSize:CGSizeMake(1000000, kScreenHeight * 0.028) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return labelFrame.size.width;
}


@end
